package com.tul.exercise.base.fanclass.eg3;

import static com.tul.exercise.base.utils.Print.print;

/**
 * @Description 《java编程思想》15 泛型.1 示例 4
 *  泛型的堆栈类
 *
 **/
public class LinkedStack<T> {
    private class Node {
        private T value;

        private Node next;

        Node() {
            value = null;
            next = null;
        }
        Node(T value, Node next) {
            this.value = value;
            this.next = next;

        }
        boolean end() {
            return value == null;
        }
    }

    private Node top = new Node();

    public void push(T item) {
        top = new Node(item, top);
    }

    public T pop() {
        T result = top.value;

        if (!top.end()) {
            top = top.next;
        }
        return result;
    }

    public static void main(String[] args) {
        LinkedStack<String> stack = new LinkedStack<>();

        String str = "I don't like you";
        for(String word : str.split(" ")){
            stack.push(word);
        }

        String s ;
        while ((s = stack.pop()) != null) {
            print(s);
        }
    }
}
